import apiClient from './client'
import type {
  Announcement,
  CreateAnnouncementRequest,
  UpdateAnnouncementRequest,
  PageRequest,
  PageResponse,
} from '../types'

export const announcementService = {
  // 创建公告
  createAnnouncement: async (data: CreateAnnouncementRequest): Promise<Announcement> => {
    const response = await apiClient.post<Announcement>('/announcements', data)
    return response.data
  },

  // 获取公告详情
  getAnnouncementById: async (id: number): Promise<Announcement> => {
    const response = await apiClient.get<Announcement>(`/announcements/${id}`)
    return response.data
  },

  // 获取所有公告（分页）
  getAllAnnouncements: async (params?: PageRequest): Promise<PageResponse<Announcement>> => {
    const response = await apiClient.get<PageResponse<Announcement>>('/announcements', { params })
    return response.data
  },

  // 按状态获取公告
  getAnnouncementsByStatus: async (
    status: 'DRAFT' | 'PUBLISHED' | 'ARCHIVED',
    params?: PageRequest
  ): Promise<PageResponse<Announcement>> => {
    const response = await apiClient.get<PageResponse<Announcement>>(
      `/announcements/status/${status}`,
      { params }
    )
    return response.data
  },

  // 获取所有已发布的公告
  getPublishedAnnouncements: async (): Promise<Announcement[]> => {
    const response = await apiClient.get<Announcement[]>('/announcements/published')
    return response.data
  },

  // 获取作者的公告
  getAnnouncementsByAuthor: async (
    authorId: number,
    params?: PageRequest
  ): Promise<PageResponse<Announcement>> => {
    const response = await apiClient.get<PageResponse<Announcement>>(
      `/announcements/author/${authorId}`,
      { params }
    )
    return response.data
  },

  // 更新公告
  updateAnnouncement: async (
    id: number,
    data: UpdateAnnouncementRequest
  ): Promise<Announcement> => {
    const response = await apiClient.put<Announcement>(`/announcements/${id}`, data)
    return response.data
  },

  // 删除公告
  deleteAnnouncement: async (id: number): Promise<void> => {
    await apiClient.delete(`/announcements/${id}`)
  },
}
